/*
The MIT License (MIT)

Copyright (c) 2013 Mike Dapiran, Brian May, Richard Pospesel, and Bert Wierenga

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once

#include "hhdFactory.h"

#define HObjectH(className)\
    public:\
    virtual void reset();\
    IHObject* instance() const { return new className(); }\
    const char8_t* hObjectName() const { return #className; }\
    uint32_t hObjectIdInstance() const { return _hObject_id; }\
    static uint32_t hObjectId() { return _hObject_id;}\
    private:\
    void hObjectId(uint32_t in_id) { _hObject_id = in_id; }\
    static uint32_t _hObject_id;

#define HObjectCPP(className)\
    uint32_t className::_hObject_id = Factory::registerHObject<className>();


namespace hhd
{
    class IHObject
    {
    public:
        virtual ~IHObject() {}

        /**
        * Release any resources necessary before returning to the factory.  Similar to
        * destructor in concept.
        */
        virtual void reset() = 0;
        /**
        * Get a new instance of this class.
        */
        virtual IHObject* instance() const = 0;
        /**
        * Get the name of the class.
        */
        virtual const char8_t* hObjectName() const = 0;
        /**
        * Returns the unique ID of this class type.
        */
        virtual uint32_t hObjectIdInstance() const = 0;

        /**
        * Determine if this preallocated object is the same time as the template parameter
        */
        template <typename T> bool is() const
        {
            return T::hObjectId() == hObjectIdInstance();
        }

        /**
        * Faster dynamic cast for preallocated
        * Returns this as a type T pointer if it is that type, otherwise returns NULL
        */
        template <typename T>
		T* as()
        {
            return T::hObjectId() == hObjectIdInstance() ? (T*)this : (T*)NULL;
        }

        template <typename T>
		const T* as() const
        {
            T::hObjectId() == hObjectIdInstance() ? (const T*)this : (T*)NULL;
        }

    protected:
        friend class Factory;
        virtual void hObjectId(uint32_t id) = 0;
    };
}
